import unittest
import time
import pcap
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

class test_webhoover(unittest.TestCase):

    #--------------------------------
    # Helper function to count network interfaces
    #--------------------------------
    def count_interfaces(self):

        counter = 0

        if(pcap.findalldevs()):
            for name, descr, addrs, flags in pcap.findalldevs():
                if addrs:
                    counter += 1

        return counter

    #--------------------------------
    # Helper function to set select to option
    #--------------------------------
    def select_option(self, select_id, option_text):

        e = self.driver.find_element_by_id(select_id)
        for option in e.find_elements_by_tag_name('option'):
            if option.text == option_text:
                option.click()


    #--------------------------------
    # Helper function to set interface boxes
    #--------------------------------
    def set_interfaces(self, input_interface, output_interface):

        self.select_option('outputinterfaces', output_interface)
        self.select_option('inputinterfaces', input_interface)


    #--------------------------------
    # Helper function to clear the log, stop hoover and check
    #--------------------------------
    def stop_hoover(self, wait = 1):
        self.driver.find_element_by_id('clearlog').click()
        self.driver.find_element_by_id('hooverbtn').click()
        
        time.sleep(wait)

        self.assertIn("Hoover was alive ", self.driver.find_element_by_id('log').text)
        self.assertIn("Hoover shutdown", self.driver.find_element_by_id('log').text)


    def setUp(self):
        self.driver = webdriver.Firefox()

    #--------------------------------
    # Check page title that we're on webhoover
    #--------------------------------
    def test_001_title(self):
        driver = self.driver
        driver.get("http://localhost")
        self.assertIn("Webhoover", driver.title)

    #--------------------------------
    # Count fuzzers received from the backend
    #--------------------------------
    def test_002_count_fuzzers(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)
        driver.find_element_by_link_text("Fuzzing").click()
        self.select_option('fuzzing_enabled', 'enabled')
        time.sleep(1)
        fuzzers = driver.find_elements_by_name("fuzzers")

        self.assertEqual(6, len(fuzzers), "Fuzzer count not as expected, got "+str(len(fuzzers))+" instead of 6")

    #--------------------------------
    # Count output interfaces received from backend
    #--------------------------------
    def test_003_count_interfaces(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)
        output_interfaces = driver.find_elements_by_xpath("//select[@id='outputinterfaces']/descendant::option")

        self.assertEqual(self.count_interfaces(), len(output_interfaces), "Output interfaces count not as expected, got "+str(len(output_interfaces))+" instead of "+str(self.count_interfaces()))

        input_interfaces = driver.find_elements_by_xpath("//select[@id='inputinterfaces']/descendant::option")

        self.assertEqual(self.count_interfaces(), len(input_interfaces), "Input interfaces count not as expected, got "+str(len(input_interfaces))+" instead of "+str(self.count_interfaces()))

    #--------------------------------
    # Simple bridging w/ --proxy, em0 <-> wlan0
    #--------------------------------
    def test_004_proxy_bridge(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        driver.find_element_by_id('hooverbtn').click()

        time.sleep(2)

        self.assertIn("executing: python hoover/NHoover.py -i wlan0 em1 --proxy", driver.find_element_by_id('log').text)

        self.stop_hoover()

    #--------------------------------
    # Fuzzing using Rambo and Enumr with filtering etc
    #--------------------------------
    def test_005_fuzzing(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        #--------------------------------
        # Set fuzzer options
        #--------------------------------
        driver.find_element_by_link_text("Fuzzing").click()
        self.select_option('fuzzing_enabled', 'enabled')
        time.sleep(1)

        driver.find_element_by_css_selector("input[value=Rambo]").click()
        driver.find_element_by_css_selector("input[value=Flipr]").click()
        driver.find_element_by_id("fprobability").send_keys("10")
        driver.find_element_by_id("ftimer").send_keys("42")

        self.select_option('flevel', '3')

        #--------------------------------
        # Enable some dropping and filtering
        #--------------------------------
        driver.find_element_by_link_text("Drop").click()
        driver.find_element_by_css_selector("input[value=icmp]").click()

        driver.find_element_by_link_text("Filtering").click()
        driver.find_element_by_id("flt_ipsrc").send_keys("8.8.8.8")
        driver.find_element_by_id("flt_ipdst").send_keys("127.0.0.1")
        driver.find_element_by_id("flt_srcport").send_keys("123")
        driver.find_element_by_id("flt_dstport").send_keys("321")

        self.select_option('flt_protocol', 'TCP')

        driver.find_element_by_id('hooverbtn').click()

        time.sleep(2)

        #--------------------------------
        # Check that the backend parsed properly
        #--------------------------------
        self.assertIn("executing: python hoover/NHoover.py -i wlan0 em1 --proxy --fuzz Flipr Rambo --fuzzing-probability 0.1 --timer 42 --drop icmp --filter ipsrc 8.8.8.8 ipdst 127.0.0.1 srcport 123 dstport 321 proto tcp", driver.find_element_by_id('log').text)

        #--------------------------------
        # Do some fuzzin'
        #--------------------------------
        time.sleep(5)

        self.stop_hoover()

    #--------------------------------
    # Trigger and test interface error msg
    #--------------------------------
    def test_006_same_interface_error_msg(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        driver.find_element_by_id('hooverbtn').click()
        
        time.sleep(1)

        msg = driver.find_element_by_id('error_msg').text
        self.assertIn("Same interface not allowed", msg)

    #--------------------------------
    # Test settings from file
    #--------------------------------
    def test_007_settings_from_file(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        try:
            os.remove("/tmp/nhoover_test008.conf")
        except:
            pass

        f = open('/tmp/nhoover_test008.conf','w')
        f.write('#-i em1 wlan0 --proxy \n')
        f.close()

        driver.find_element_by_link_text("From File").click()
        driver.find_element_by_id("settings_from_file").send_keys("/tmp/nhoover_test008.conf")

        driver.find_element_by_id('hooverbtn').click()
        
        time.sleep(1)

        msg = driver.find_element_by_id('log').text
        self.assertIn("executing: python hoover/NHoover.py --settings /tmp/nhoover_test008.conf", msg)

        self.stop_hoover()

        try:
            os.remove("/tmp/nhoover_test008.conf")
        except:
            pass    

    #--------------------------------
    # Test logging and overwriting msgs
    #--------------------------------
    def test_008_logging(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        #--------------------------------
        # Configure logging options
        #--------------------------------
        driver.find_element_by_link_text("Logging").click()
        driver.find_element_by_id("loutput").send_keys("/tmp/log.pcap")

        self.select_option('lformat', 'PCAP')

        time.sleep(1)

        self.select_option('lunaltered', 'log everything')

        try:
            os.remove("/tmp/log.pcap")
        except:
            pass

        driver.find_element_by_id('hooverbtn').click()
        
        time.sleep(1)

        msg = driver.find_element_by_id('log').text
        self.assertIn("executing: python hoover/NHoover.py -i wlan0 em1 --proxy --log /tmp/log.pcap pcap --log-unaltered", msg)

        time.sleep(1)

        self.stop_hoover(3)

        #--------------------------------
        # Re-check and verify log overwrite
        #--------------------------------
        driver.find_element_by_id('clearlog').click()
        driver.find_element_by_id('hooverbtn').click()

        time.sleep(1)

        msg = driver.find_element_by_id('log').text
        self.assertIn("executing: python hoover/NHoover.py -i wlan0 em1 --proxy --log /tmp/log.pcap pcap --log-unaltered", msg)

        time.sleep(1)
        msg = driver.find_element_by_id('question_msg').text
        self.assertIn("Defined logfile already exists. Pcap-format doesn't support files with multiple sessions. Do you want to overwrite file '/tmp/log.pcap'?", msg)
        driver.find_element_by_id('question_yes_btn').click()

        time.sleep(1)
        self.assertIn("Also logging unaltered stream to a file '/tmp/log.pcap_unaltered'", driver.find_element_by_id('log').text)
        
        self.stop_hoover(3)

        os.remove("/tmp/log.pcap");

        os.remove("/tmp/log.pcap_unaltered");

        #--------------------------------
        # Re-check with Hoover format
        #--------------------------------
        try:
            os.unlink("/tmp/log.hvr")
        except:
            pass

        driver.find_element_by_id("loutput").clear()
        driver.find_element_by_id("loutput").send_keys("/tmp/log.hvr")

        self.select_option('lformat', 'Hoover')
        self.select_option('lverbose', 'enabled')

        driver.find_element_by_id('clearlog').click()
        driver.find_element_by_id('hooverbtn').click()

        time.sleep(1)

        msg = driver.find_element_by_id('log').text
        self.assertIn("executing: python hoover/NHoover.py -i wlan0 em1 --proxy --log /tmp/log.hvr hoover true", msg)

        self.stop_hoover(1)

        #--------------------------------
        # Test Hoover log appending
        #--------------------------------
        driver.find_element_by_id('clearlog').click()
        time.sleep(2)

        driver.find_element_by_id('hooverbtn').click()
        time.sleep(1)

        msg = driver.find_element_by_id('question_msg').text

        self.assertIn("Defined logfile already exists. Do you want to add all the new logging information to the end of the defined file?", msg)
        driver.find_element_by_id('question_yes_btn').click()

        self.stop_hoover(2)

        os.unlink("/tmp/log.hvr");

    #--------------------------------
    # Test spoofing settings
    #--------------------------------
    def test_009_spoofing(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        #--------------------------------
        # Configure spoofing options
        #--------------------------------
        driver.find_element_by_link_text("Spoofing").click()
        driver.find_element_by_id("s_ipsrc").send_keys("1.2.3.4")
        driver.find_element_by_id("s_ipdst").send_keys("4.3.2.1")
        driver.find_element_by_id("s_ip6src").send_keys("2001:db8:85a3:0:0:8a2e:370:7334")
        driver.find_element_by_id("s_ip6dst").send_keys("0:0:0:0:0:0:0:1")
        driver.find_element_by_id("s_ethsrc").send_keys("erroneous_adr")
        driver.find_element_by_id("s_ethdst").send_keys("01:23:45:67:89:ab")

        driver.find_element_by_id('hooverbtn').click()
        
        time.sleep(1)

        msg = driver.find_element_by_id('log').text
        self.assertIn("executing: python hoover/NHoover.py -i wlan0 em1 --proxy", msg)
        self.assertIn("--spoofing ipsrc 1.2.3.4 ipdst 4.3.2.1 ip6src 2001:db8:85a3:0:0:8a2e:370:7334 ip6dst 0:0:0:0:0:0:0:1 ethsrc erroneous_adr ethdst 01:23:45:67:89:ab", msg)
        self.assertIn("Erronous ethernet address: 'ethsrc'. ignoring the value.", msg)

        time.sleep(1)

        self.stop_hoover()

    #--------------------------------
    # Verify all drop checkboxes
    #--------------------------------
    def test_010_drop(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        driver.find_element_by_link_text("Drop").click()
        driver.find_element_by_css_selector("input[value=icmp]").click()
        driver.find_element_by_css_selector("input[value=arp]").click()
        driver.find_element_by_css_selector("input[value=udp]").click()
        driver.find_element_by_css_selector("input[value=tcp]").click()

        #--------------------------------
        # Perform the test
        #--------------------------------
        driver.find_element_by_id('clearlog').click()
        driver.find_element_by_id('hooverbtn').click()
        time.sleep(1)
        
        msg = driver.find_element_by_id('log').text
        self.assertIn("executing: python hoover/NHoover.py -i wlan0 em1 --proxy --drop tcp arp udp icmp", msg)

        self.stop_hoover()


    #--------------------------------
    # Verify missing fprobability notification
    #--------------------------------
    def test_011_missing_fprobability(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        driver.find_element_by_link_text("Fuzzing").click()
        self.select_option('fuzzing_enabled', 'enabled')
        time.sleep(1)

        driver.find_element_by_css_selector("input[value=Rambo]").click()

        driver.find_element_by_id('hooverbtn').click()
        time.sleep(1)
        
        msg = driver.find_element_by_id('error_msg').text
        self.assertIn("Fuzzing probability needs to be between 1% and 100%!", msg)

        driver.find_element_by_id('close_error').click()

    #--------------------------------
    # Verify missing fprobability notification
    #--------------------------------
    def test_012_invalid_timer(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        driver.find_element_by_link_text("Fuzzing").click()
        self.select_option('fuzzing_enabled', 'enabled')
        time.sleep(1)

        driver.find_element_by_css_selector("input[value=Rambo]").click()

        driver.find_element_by_id("fprobability").send_keys("42")
        driver.find_element_by_id("ftimer").send_keys("abc")

        driver.find_element_by_id('hooverbtn').click()
        time.sleep(1)
        
        msg = driver.find_element_by_id('error_msg').text
        self.assertIn("Invalid timer value!", msg)

        driver.find_element_by_id('close_error').click()

    #--------------------------------
    # Verify missing fprobability notification
    #--------------------------------
    def test_013_additional_fuzzopts(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        driver.find_element_by_link_text("Fuzzing").click()
        self.select_option('fuzzing_enabled', 'enabled')
        time.sleep(1)

        driver.find_element_by_css_selector("input[value=Rambo]").click()

        driver.find_element_by_id("fprobability").send_keys("42")
        driver.find_element_by_id("fuzzing_options").send_keys("opt1 opt2")

        driver.find_element_by_id('hooverbtn').click()
        time.sleep(1)
        
        msg = driver.find_element_by_id('log').text
        self.assertIn("executing: python hoover/NHoover.py -i wlan0 em1 --proxy --fuzz Rambo --fuzzing-probability 0.42 --fuzzing-options opt1 opt2", msg)

        self.stop_hoover()

    #--------------------------------
    # Test replay inputs
    #--------------------------------
    def test_014_replay(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        self.select_option('replay', 'enabled')
        time.sleep(1)

        driver.find_element_by_id("replay_from").send_keys("/tmp/not_available")
        driver.find_element_by_id("replay_interval").send_keys("13")

        driver.find_element_by_id('hooverbtn').click()
        time.sleep(1)
        
        msg = driver.find_element_by_id('log').text
        self.assertIn("executing: python hoover/NHoover.py --replay /tmp/not_available em1 13", msg)

        msg = driver.find_element_by_id('error_msg').text
        self.assertIn("File can't be processed.", msg)

        driver.find_element_by_id('close_error').click()

        driver.find_element_by_id("replay_interval").clear()

        driver.find_element_by_id('hooverbtn').click()
        time.sleep(1)
        
        msg = driver.find_element_by_id('log').text
        self.assertIn("executing: python hoover/NHoover.py --replay /tmp/not_available em1", msg)

        msg = driver.find_element_by_id('error_msg').text
        self.assertIn("File can't be processed.", msg)

        driver.find_element_by_id('close_error').click()

    #--------------------------------
    # Verify missing duplicate% notification
    #--------------------------------
    def test_015_missing_duplicateprob(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        driver.find_element_by_link_text("Fuzzing").click()
        self.select_option('fuzzing_enabled', 'enabled')
        time.sleep(1)

        driver.find_element_by_id("duplicate").clear()
        driver.find_element_by_css_selector("input[value=Duplicate]").click()

        driver.find_element_by_id('hooverbtn').click()
        time.sleep(1)
        
        msg = driver.find_element_by_id('error_msg').text
        self.assertIn("Duplicate probability needs to be between 1% and 100%!", msg)

        driver.find_element_by_id('close_error').click()

    #--------------------------------
    # Verify duplicate% input
    #--------------------------------
    def test_016_duplicate_probability(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        driver.find_element_by_link_text("Fuzzing").click()
        self.select_option('fuzzing_enabled', 'enabled')
        time.sleep(1)

        driver.find_element_by_id("duplicate").send_keys("50")
        driver.find_element_by_css_selector("input[value=Duplicate]").click()

        driver.find_element_by_id('hooverbtn').click()
        time.sleep(1)
        
        msg = driver.find_element_by_id('log').text
        self.assertIn("executing: python hoover/NHoover.py -i wlan0 em1 --proxy --fuzz Duplicate --duplicate-probability 0.5", msg)

        self.stop_hoover()

    #--------------------------------
    # Verifies run time controls
    #--------------------------------
    def test_017_runtime_controls(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        #----------------------------
        # Set fuzzing options
        #----------------------------
        driver.find_element_by_link_text("Fuzzing").click()
        self.select_option('fuzzing_enabled', 'enabled')
        time.sleep(1)

        driver.find_element_by_css_selector("input[value=Enumr]").click()
        driver.find_element_by_css_selector("input[value=Rambo]").click()

        driver.find_element_by_id("fprobability").send_keys("13")
        driver.find_element_by_id("ftimer").send_keys("1")

        #----------------------------
        # Launch hoover
        #----------------------------
        driver.find_element_by_id('hooverbtn').click()
        time.sleep(1)
        
        msg = driver.find_element_by_id('log').text
        self.assertIn("executing: python hoover/NHoover.py -i wlan0 em1 --proxy --fuzz Enumr Rambo --fuzzing-probability 0.13 --timer 1", msg)
        self.driver.find_element_by_id('clearlog').click()

        #----------------------------
        # Test "Swap" button
        #----------------------------
        driver.find_element_by_id('swap').click()
        time.sleep(1)

        self.assertIn("Swapping complete.", driver.find_element_by_id('log').text)

        #----------------------------
        # Test "Packet details" button
        #----------------------------
        driver.find_element_by_id('packetdetails').click()
        time.sleep(1)

        self.assertIn("-- Enabling information display --", driver.find_element_by_id('log').text)

        #----------------------------
        # Test "Settings" button
        #----------------------------
        driver.find_element_by_id('settings').click()
        time.sleep(1)

        self.assertIn("Network hoover's currently used settings.", driver.find_element_by_id('log').text)
        self.assertIn("Listening interface: em1", driver.find_element_by_id('log').text)
        self.assertIn("Output interface: wlan0", driver.find_element_by_id('log').text)
        self.assertIn("Proxy mode is ON, also listening interface wlan0", driver.find_element_by_id('log').text)
        self.assertIn("Packet filtering is not enabled.", driver.find_element_by_id('log').text)
        self.assertIn("Fuzzing is enabled and the following fuzzers are utilized:", driver.find_element_by_id('log').text)
        self.assertIn("Enumr", driver.find_element_by_id('log').text)
        self.assertIn("Rambo", driver.find_element_by_id('log').text)
        self.assertIn("Currently used fuzzer: Enumr", driver.find_element_by_id('log').text)
        self.assertIn("Fuzzing probability: 0.13", driver.find_element_by_id('log').text)
        self.assertIn("Logging is not enabled.", driver.find_element_by_id('log').text)
        self.assertIn("Fuzzer timer is defined, interval is 1 seconds.", driver.find_element_by_id('log').text)

        self.driver.find_element_by_id('clearlog').click()

        #----------------------------
        # Test "Timer" button
        #----------------------------
        driver.find_element_by_id('timer').click()
        time.sleep(5)

        self.assertIn("-- Fuzzer timer ON --", driver.find_element_by_id('log').text)
        self.assertIn("Now fuzzing with: None", driver.find_element_by_id('log').text)
        self.assertIn("Now fuzzing with: Enumr", driver.find_element_by_id('log').text)
        self.assertIn("Now fuzzing with: Rambo", driver.find_element_by_id('log').text)
        driver.find_element_by_id('timer').click()
        self.assertIn("-- Fuzzer timer OFF --", driver.find_element_by_id('log').text)
        self.driver.find_element_by_id('clearlog').click()

        #----------------------------
        # Test "Change Fuzzer" buttons
        #----------------------------
        self.driver.find_element_by_id('changefuzzer').click()
        self.assertIn("Now fuzzing with:", driver.find_element_by_id('log').text)
        self.stop_hoover()


    #--------------------------------
    # Test the proxy switch
    #--------------------------------
    def test_018_proxy_switch(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')
        self.select_option('proxy', 'disabled')

        driver.find_element_by_id('clearlog').click()
        driver.find_element_by_id('hooverbtn').click()

        msg = driver.find_element_by_id('log').text
        self.assertIn("executing: python hoover/NHoover.py -i wlan0 em1", msg)
        self.assertNotIn("--proxy", msg)

        self.stop_hoover()
      
    #--------------------------------
    # Partial filtering spoofing settings
    #--------------------------------
    def test_019_partial_filtering_spoofing(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        #--------------------------------
        # Configure some spoofing options
        #--------------------------------
        driver.find_element_by_link_text("Spoofing").click()
        driver.find_element_by_id("s_ipsrc").send_keys("1.2.3.4")
        driver.find_element_by_id("s_ip6src").send_keys("2001:db8:85a3:0:0:8a2e:370:7334")

        #--------------------------------
        # Enable some filtering
        #--------------------------------
        driver.find_element_by_link_text("Filtering").click()
        driver.find_element_by_id("flt_ipsrc").send_keys("1.2.3.4")

        driver.find_element_by_id('clearlog').click()
        driver.find_element_by_id('hooverbtn').click()

        msg = driver.find_element_by_id('log').text
        self.assertIn("executing: python hoover/NHoover.py -i wlan0 em1 --proxy --filter ipsrc 1.2.3.4 --spoofing ipsrc 1.2.3.4 ip6src 2001:db8:85a3:0:0:8a2e:370:7334", msg)

        self.stop_hoover()

    #--------------------------------
    # Test form saving/restoring/deleting
    #--------------------------------
    def test_020_form_save_restore_delete(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        #-----------------------------
        # Clear the local storage first
        #-----------------------------
        driver.find_element_by_link_text("Save/Load").click()  
        self.driver.find_element_by_id('clear_storage').click()

        self.assertIn("All saved settings have been deleted!", self.driver.find_element_by_id('info_msg').text)

        self.driver.find_element_by_id('info_close').click()

        #-----------------------------
        # Save defaults as "defaults"
        #-----------------------------
        driver.find_element_by_id("settings_title").send_keys("defaults")
        self.driver.find_element_by_id('save_settings').click()

        self.assertIn("Current settings have been saved as defaults!", self.driver.find_element_by_id('info_msg').text)

        self.driver.find_element_by_id('info_close').click()

        #-----------------------------
        # Check few DROP options
        #-----------------------------
        driver.find_element_by_link_text("Drop").click()
        driver.find_element_by_css_selector("input[value=icmp]").click()
        driver.find_element_by_css_selector("input[value=arp]").click()

        #-----------------------------
        # Load defaults
        #-----------------------------
        driver.find_element_by_link_text("Save/Load").click()
        self.select_option('storage_settings_load', 'defaults')
        self.driver.find_element_by_id('storage_settings_load_btn').click()

        self.assertIn("Setting defaults has been restored!", self.driver.find_element_by_id('info_msg').text)

        #-----------------------------
        # Verify the DROPs got unchecked
        #-----------------------------
        driver.find_element_by_link_text("Drop").click()
        self.assertFalse(driver.find_element_by_css_selector("input[value=icmp]").is_selected())
        self.assertFalse(driver.find_element_by_css_selector("input[value=arp]").is_selected())

        driver.get("http://localhost")

        time.sleep(1)

        #-----------------------------
        # Check there is one saved setting to delete
        #-----------------------------
        driver.find_element_by_link_text("Save/Load").click()  
        options = driver.find_elements_by_xpath("//select[@id='storage_settings_delete']/descendant::option")

        self.assertEqual(1, len(options), "Settings count unexpected, got "+str(len(options))+" instead of 1")

        #-----------------------------
        # Delete it
        #-----------------------------
        self.select_option('storage_settings_delete', 'defaults')
        self.driver.find_element_by_id('storage_settings_delete_btn').click()

        self.assertIn("Saved setting defaults has been deleted!", self.driver.find_element_by_id('info_msg').text)
        self.driver.find_element_by_id('info_close').click()

        #-----------------------------
        # Check there is no saves left to load
        #-----------------------------
        options = driver.find_elements_by_xpath("//select[@id='storage_settings_delete']/descendant::option")

        self.assertEqual(0, len(options), "Settings count unexpected, got "+str(len(options))+" instead of 0")


    #--------------------------------
    # Verifies Fuzzing Disabled setting
    #--------------------------------
    def test_021_fuzzing_disabled(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        #--------------------------------
        # Set fuzzer options
        #--------------------------------
        driver.find_element_by_link_text("Fuzzing").click()
        self.select_option('fuzzing_enabled', 'enabled')
        time.sleep(1)

        driver.find_element_by_css_selector("input[value=Rambo]").click()
        driver.find_element_by_css_selector("input[value=Flipr]").click()
        driver.find_element_by_id("fprobability").send_keys("10")

        driver.find_element_by_id('hooverbtn').click()

        time.sleep(2)

        #--------------------------------
        # Check that the backend parsed properly
        #--------------------------------
        self.assertIn("executing: python hoover/NHoover.py -i wlan0 em1 --proxy --fuzz Flipr Rambo --fuzzing-probability 0.1", driver.find_element_by_id('log').text)

        #--------------------------------
        # Do some fuzzin'
        #--------------------------------
        time.sleep(5)

        self.stop_hoover()

        #--------------------------------
        # Disabled fuzzing
        #--------------------------------
        driver.find_element_by_link_text("Fuzzing").click()
        self.select_option('fuzzing_enabled', 'disabled')
        time.sleep(1)
        driver.find_element_by_id('hooverbtn').click()

        self.assertIn("executing: python hoover/NHoover.py -i wlan0 em1 --proxy", driver.find_element_by_id('log').text)

        self.stop_hoover()

    #--------------------------------
    # Erroneous replay inputs
    #--------------------------------
    def test_022_erroneous_reply_input(self):
        driver = self.driver
        driver.get("http://localhost")

        time.sleep(1)

        self.set_interfaces('wlan0', 'em1')

        #--------------------------------
        # Set replay options, missing input
        #--------------------------------
        self.select_option('replay', 'enabled')
        time.sleep(1)

        driver.find_element_by_id("replay_from").clear()

        driver.find_element_by_id('hooverbtn').click()

        self.assertIn("No replay file defined!", self.driver.find_element_by_id('error_msg').text)
        driver.find_element_by_id('close_error').click()

        #--------------------------------
        # Set replay options, invalid interval
        #--------------------------------
        driver.find_element_by_id("replay_from").send_keys("/tmp/log")
        driver.find_element_by_id("replay_interval").send_keys("!0")

        driver.find_element_by_id('hooverbtn').click()

        self.assertIn("Replay interval needs to be a positive number!", self.driver.find_element_by_id('error_msg').text)
        driver.find_element_by_id('close_error').click()

    def tearDown(self):
        self.driver.close()


if __name__ == "__main__":
    unittest.main()
